<template>
  <a-modal :open="open" width="1200px" title="检验接口维护" centered :ok-button-props="{ disabled: !isCompleted }" @cancel="onCancel" @ok="onSave">
    <div class="mb-2">
      <a-button type="primary" class="" @click="getData">生成接口 </a-button>
      <a-button type="primary" class="ml-2" danger @click="onDelAll">全部删除 </a-button>
    </div>
    <BaseTable :data-source="tableData" :height="637" :loading="loading" column-code="000483">
      <template #bodyCell="{ column, record, index }">
        <template v-if="column.dataIndex === 'NO'">
          {{ index + 1 }}
        </template>
        <template v-if="column.dataIndex === 'outorder' && index > 3">
          <EditCell title="顺序号">
            <BaseInput v-model:value="record.outorder" placeholder="请输入" @blur="onSave(record)" />
          </EditCell>
        </template>

        <template v-if="column.dataIndex === 'startPosition' && index > 3">
          <EditCell title="起始位置">
            <BaseInput v-model:value="record.startPosition" placeholder="请输入" @blur="onSave(record)" />
          </EditCell>
        </template>

        <template v-if="column.dataIndex === 'truncateLength' && index > 3">
          <EditCell title="截取长度">
            <BaseInput v-model:value="record.truncateLength" placeholder="请输入" @blur="onSave(record)" />
          </EditCell>
        </template>

        <template v-if="column.dataIndex === 'scaleFactor' && index > 3">
          <EditCell title="缩放系数">
            <BaseInput v-model:value="record.scaleFactor" placeholder="请输入" @blur="onSave(record)" />
          </EditCell>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <a-space>
            <a-button v-if="index > 3" type="link" size="small" danger @click="onDel(record)"> 删除 </a-button>
          </a-space>
        </template>
      </template>
    </BaseTable>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const formState = ref({})
const tableData = ref([])
const loading = ref(false)
const page = ref({ total: 0, current: 1, pageSize: 999 })

const props = defineProps({
  open: Boolean,
  state: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const getData = async () => {
  const { code, msg } = await post('/lis/InstrumentType/generateInspectionInterface', { lisInstrumentTypeId: props.rowData.id })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  queryList()
}
const onDelAll = async () => {
  const { code, msg } = await post('/lis/InstrumentType/delAllInspectionInterface', { lisInstrumentTypeId: props.rowData.id })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  queryList()
}
const onDel = async (row) => {
  const { code, msg } = await post('/lis/InstrumentType/delInspectionInterface', { row })
  if (code !== 0) return message.error(msg)
  message.success(msg)
}
const queryList = async () => {
  loading.value = true
  const { code, data, total, msg } = await post('/lis/InstrumentType/getInspectionInterfaceList', {
    lisInstrumentTypeId: props.rowData.id
  })
  if (code !== 0) return message.error(msg)
  tableData.value = data
  page.value.total = total
  loading.value = false
}
const onSave = async (row) => {
  const { code, msg } = await post('/lis/InstrumentType/saveLisInstrumentTypeInterface', row)
  if (code !== 0) return message.error(msg)
  message.success(msg)
  queryList()
}

const onCancel = () => {
  emit('update:open', false)
  emit('success')
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value = { ...props.rowData }
      queryList()
    }
  },
  {
    immediate: true
  }
)
</script>
